package com.yuanli.cosmeticmirror.view.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.kongzue.dialog.v3.WaitDialog;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;

import butterknife.BindView;
import butterknife.OnClick;

import static com.yuanli.cosmeticmirror.base.Constant.USER_AGREEMENT_PRIVACY_POLICY;

/**
 * 用户协议/隐私政策
 */
public class ProtocolActivity extends BaseActivity {
    @BindView(R.id.about_back)
    ImageView aboutBack;
    @BindView(R.id.tv_agreement_content)
    TextView tvTermsContent;
    @BindView(R.id.protocol_title)
    TextView protocolTitle;

    @BindView(R.id.web_view)
    WebView mWebView;
    @BindView(R.id.scrollView)
    ScrollView scrollView;


    @Override
    protected int getContentView() {
        return R.layout.activity_protocol;
    }

    @Override
    protected void init(Bundle bundle) {
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        boolean isUserAgreement = getUserAgreement();
        setBarTitle(isUserAgreement);

        initWebView(isUserAgreement);
    }

    @Override
    protected void destroy() {
        if (mWebView != null){
            mWebView.destroy();
            mWebView = null;
        }
    }


    @OnClick(R.id.about_back)
    public void onViewClicked() {
        finish();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 设置标题栏
     * @param isUserAgreement
     */
    private void setBarTitle(boolean isUserAgreement){
        protocolTitle.setText(isUserAgreement ? "用户协议" : "隐私政策");
    }


    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView(boolean isUserAgreement){
        mWebView.loadUrl(getUrl(isUserAgreement));
        //禁止缩放
        mWebView.getSettings().setSupportZoom(false);
        //如果访问的页面中要与Javascript交互，则webView必须设置支持Javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 缩放至屏幕的大小
        mWebView.getSettings().setLoadWithOverviewMode(true);
        //不使用缓存
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        //设置编码格式
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                if (request != null && request.getUrl() != null){
                    String uri = request.getUrl().toString();
                    String reqUrl = getString(R.string.agreement_base_url);
                    if (uri.contains(reqUrl)){
                        mWebView.setVisibility(View.GONE);
                        initTvContent(isUserAgreement);
                    }
                }
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                WaitDialog.showWait(ProtocolActivity.this, "正在加载...").setCancelable(false);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                WaitDialog.dismiss();
            }
        });
    }

    private void initTvContent(boolean isUserAgreement){
        scrollView.setVisibility(View.VISIBLE);
        if (isUserAgreement){
            tvTermsContent.setText(R.string.user_agreement_content);
        }else{
            tvTermsContent.setText(R.string.privacy_policy_content);
        }
    }

    private String getUrl(boolean isUserAgreement){
        String appName = getString(R.string.app_name);
        return getString(isUserAgreement ?
                R.string.user_agreement_url :
                R.string.privacy_policy_url, appName);
    }

    private boolean getUserAgreement(){
        int booleanExtra = getIntent().getIntExtra(USER_AGREEMENT_PRIVACY_POLICY, 1);
        return booleanExtra == 1;
    }

}
